package dth.dao;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

import dth.entity.FeedBack;

public class FeedBackDAO {
	private SessionFactory sessionFactory=new AnnotationConfiguration().configure().buildSessionFactory();
	private Session session=sessionFactory.openSession();
	
	public java.util.List<FeedBack> getListFeedBack(){
		session.beginTransaction();
		String queryString="select * from feedback";
		Query query=session.createSQLQuery(queryString).addEntity(FeedBack.class);
		return query.list();
	}
	public int delete(int id)
	{
		Transaction tx=session.beginTransaction();
		String queryString="delete from feedback where feedback_id= :id";
		Query query=session.createSQLQuery(queryString).setParameter("id", id);
		int count=query.executeUpdate();
		tx.commit();
		return count;
	}
	public int insert(FeedBack feedback)
	{
		Transaction tx=session.beginTransaction();
		String queryString="INSERT into feedback VALUES(0,:content,:fk_user_id,:date_insert,:title)";
		System.out.println("-----INSERT into feedback VALUES(0"+feedback.getFeedbackTitle()+","+feedback.getFeedbackContent()+","+feedback.getFkUserId());
		Query query=session.createSQLQuery(queryString);
		query.setParameter("title",feedback.getFeedbackTitle());
		query.setParameter("content",feedback.getFeedbackContent());
		query.setParameter("fk_user_id",feedback.getFkUserId());
		query.setParameter("date_insert",feedback.getDateInsert());
		int count=query.executeUpdate();
		tx.commit();
		return count;
	}
	public int update(FeedBack feedback)
	{
		Transaction tx=session.beginTransaction();
		String queryString="update feedback set feedback_content=:content,fk_user_id=:fk_user_id,date_insert=:date,feedback_title=:title where feedback_id=:id";
		Query query=session.createSQLQuery(queryString);
		query.setParameter("title",feedback.getFeedbackTitle());
		query.setParameter("content",feedback.getFeedbackContent());
		query.setParameter("id",feedback.getFeedbackId());
		query.setParameter("fk_user_id",feedback.getFkUserId());
		query.setParameter("date",feedback.getDateInsert());
		int count=query.executeUpdate();
		tx.commit();
		return count;
	}
}
